/*
 * @Author: 吕会录 lvhuilu125807@163.com
 * @Date: 2023-08-09 11:31:07
 * @LastEditors: 吕会录 lvhuilu125807@163.com
 * @LastEditTime: 2023-08-15 16:50:16
 * @FilePath: \xyhdp\sokect\index.js
 * @Description: 
 * Copyright (c) 2023 by ${git_name} email: ${git_email}, All Rights Reserved.
 */
const socketService = {
	socket: null,
	init(username) {
		if (typeof WebSocket === "undefined") {
			alert("您的浏览器不支持socket");
		} else {
			let path = "ws://47.97.107.54:8089/dashboard/websocket/" + username; // 请求路径
			// let path = "http://test.care-bay.com/dashboard/websocket/" + username; // 请求路径

			this.socket = new WebSocket(path);
			this.socket.onopen = this.open.bind(this);
			this.socket.onerror = this.error.bind(this);
			this.socket.onmessage = this.getMessage.bind(this);
		}
	},

	open() {
		console.log("预警socket连接成功");
	},

	error() {
		console.log("连接错误");
	},

	getMessage(callback) {
		this.socket.onmessage = (msg) => {
			console.log(msg.data);
			if (msg.data !== '连接成功') {
				console.log("454545", msg.data);
				callback(msg.data); // 调用传递的回调函数并传递数据
			}
		};
	},

	// getMessage() {
	// 	return new Promise((resolve, reject) => {
	// 		this.socket.onmessage = (msg) => {
	// 			console.log(msg.data);
	// 			if (msg.data != '连接成功') {
	// 				// const data = JSON.parse(msg.data);
	// 				console.log("454545", msg.data);
	// 				resolve(msg.data); // 将数据传递给调用者
	// 			}

	// 			// this.scrollInstance.refresh(); // 手动刷新滚动效果

	// 		};
	// 	});
	// 	// this.scrollInstance.refresh(); // 手动刷新滚动效果
	// },

	send(params) {
		if (this.socket) {
			this.socket.send(params);
		}
	},

	close() {
		console.log("socket已经关闭");
	}
};

export default socketService;